package com.jic.member.impl;
import java.math.BigDecimal;
import java.util.*;

import java.lang.Long;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.vo.AdminOperate;
import com.jic.common.base.vo.RestResult;
import com.jic.member.request.EventListenerRequest;
import com.jic.manager.api.manage.SysDeptApi;
import com.jic.manager.request.ChildNameRequest;
import com.jic.manager.request.SysDeptRequest;
import com.jic.manager.response.MemMerchantInfoResponse;
import com.jic.manager.response.SysDeptResponse;
import com.jic.member.common.constant.GlobalConsts;
import com.jic.member.common.util.BeanUtil;
import com.jic.member.entity.*;
import com.jic.member.enums.EXCEPTION;
import com.jic.member.enums.GetTypeEnum;
import com.jic.member.mapper.*;
import com.jic.member.request.*;
import com.jic.member.response.*;
import com.jic.member.service.*;
import com.jic.common.base.vo.PageResult;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 会员等级管理-成长值规则主表
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-04-08 03:50:24
 */
@Service
@Slf4j
public class MememberGrowthRuleServiceImpl implements MememberGrowthRuleService {
    @Resource
    private MememberGrowthRuleMapper mememberGrowthRuleMapper;
    @Resource
    MememberGrowthActionService mememberGrowthActionService;
    @Resource
    MememberGrowthOrderRechargeService mememberGrowthOrderRechargeService;
    @Resource
    MememberGrowthOrderMoneyService mememberGrowthOrderMoneyService;
    @Resource
    MememberGrowthAppraisalService mememberGrowthAppraisalService;
    @Resource
    private MememberUserMapper mememberUserMapper;
    @Resource
    MemMemberGrowthBaseMapper mememberGrowthBaseMapper;
    @Resource
    private SysDeptApi sysDeptApi;
    @Resource
    private MememberGrowthLogMapper mememberGrowthLogMapper;
    @Resource
    MememberGradeMapper mememberGradeMapper;


    @Override
    public RestResult<Long> insert(MemberGrowthRuleRequest param) {
        RestResult result = new RestResult();
        MememberGrowthRule mememberGrowthRule = new MememberGrowthRule();
        // 获取随机成长值编码
        Date dt = new Date();
        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        String formatdate = df.format(dt); //获取年月日
        int num = (int) ((Math.random() * 9 + 1) * 1000); //获取随机四位数
        /*先判断当前操作账号所属组织*/
        Integer deptType = param.getAdminOperate().getDeptType().getCode();
        /*如果是子，母平台则直接新增规则主表*/
        if (deptType == 0) {
            // 保存成长值规则主表
            mememberGrowthRule.setCreateTime(new Date());
            mememberGrowthRule.setCreator(param.getAdminOperate().getOperator());
            mememberGrowthRule.setCreatorName(param.getAdminOperate().getOperateName());
            mememberGrowthRule.setDeleteFlag(0);
            mememberGrowthRule.setDeptId(param.getAdminOperate().getDeptId());
            mememberGrowthRule.setObtainMode(param.getObtainMode());
            mememberGrowthRule.setRuleName(param.getRuleName());
            mememberGrowthRule.setStatus(1);
            mememberGrowthRule.setWhetherPic(param.getWhetherPic());
            mememberGrowthRule.setDeptType(Integer.valueOf(param.getAdminOperate().getDeptType().getCode()));
            mememberGrowthRule.setGrowthNum("CZ" + formatdate + num + param.getAdminOperate().getDeptId());
            long flag = mememberGrowthRuleMapper.insert(mememberGrowthRule);
            if (flag != 0) {
                result.setData(flag);
                result.setCode("0000");
            } else {
                result.setCode("9999");
                result.setMsg("保存规则主表失败");
                return result;
            }
        } else if (deptType == 1) {
            // 保存成长值规则主表
            mememberGrowthRule.setCreateTime(new Date());
            mememberGrowthRule.setCreator(param.getAdminOperate().getOperator());
            mememberGrowthRule.setCreatorName(param.getAdminOperate().getOperateName());
            mememberGrowthRule.setDeleteFlag(0);
            mememberGrowthRule.setDeptId(param.getAdminOperate().getDeptId());
            mememberGrowthRule.setObtainMode(param.getObtainMode());
            mememberGrowthRule.setRuleName(param.getRuleName());
            mememberGrowthRule.setStatus(1);
            mememberGrowthRule.setWhetherPic(param.getWhetherPic());
            mememberGrowthRule.setDeptType(Integer.valueOf(param.getAdminOperate().getDeptType().getCode()));
            mememberGrowthRule.setGrowthNum("CZ" + formatdate + num + param.getAdminOperate().getDeptId());
            mememberGrowthRule.setChildId(param.getAdminOperate().getChildIds().get(0));
            long flag = mememberGrowthRuleMapper.insert(mememberGrowthRule);
            if (flag != 0) {
                result.setData(flag);
                result.setCode("0000");
            } else {
                result.setCode("9999");
                result.setMsg("保存规则主表失败");
                return result;
            }
        } else if (deptType == 2) { //如果是商家 则判断是否设置了适用范围

            MemMerchantInfoResponse memMerchantInfoResponse = sysDeptApi.getGrowhtScopeInfo(param.getAdminOperate().getMerchantIds().get(0));
            Long scopeApplication = memMerchantInfoResponse.getScopeApplication();
            if (scopeApplication != 0) { //如果设置了适用范围则可以新增
                // 保存成长值规则主表
                mememberGrowthRule.setCreateTime(new Date());
                mememberGrowthRule.setCreator(param.getAdminOperate().getOperator());
                mememberGrowthRule.setCreatorName(param.getAdminOperate().getOperateName());
                mememberGrowthRule.setDeleteFlag(0);
                mememberGrowthRule.setDeptId(param.getAdminOperate().getDeptId());
                mememberGrowthRule.setObtainMode(param.getObtainMode());
                mememberGrowthRule.setRuleName(param.getRuleName());
                mememberGrowthRule.setStatus(1);
                mememberGrowthRule.setWhetherPic(param.getWhetherPic());
                mememberGrowthRule.setDeptType(Integer.valueOf(param.getAdminOperate().getDeptType().getCode()));
                mememberGrowthRule.setGrowthNum("CZ" + formatdate + num + param.getAdminOperate().getDeptId());
                mememberGrowthRule.setMerchantId(param.getAdminOperate().getMerchantIds().get(0));
                mememberGrowthRule.setChildId(Long.valueOf(param.getAdminOperate().getBusinessId()));
                long flag = mememberGrowthRuleMapper.insert(mememberGrowthRule);
                if (flag != 0) {
                    result.setData(flag);
                    result.setCode("0000");
                } else {
                    result.setCode("9999");
                    result.setMsg("保存规则主表失败");
                    return result;
                }
            } else {
                result.setCode("8888");
                result.setMsg("请先设置【等级适用范围】，之后按照权限进行操作");
                return result;
            }
        } else if (deptType == 3) {
            MemMerchantInfoResponse memMerchantInfoResponse = sysDeptApi.getGrowhtScopeInfo(param.getAdminOperate().getMerchantIds().get(0));
            Long scopeApplication = memMerchantInfoResponse.getScopeApplication();
            if (scopeApplication != 0) {
                // 保存成长值规则主表
                mememberGrowthRule.setCreateTime(new Date());
                mememberGrowthRule.setCreator(param.getAdminOperate().getOperator());
                mememberGrowthRule.setCreatorName(param.getAdminOperate().getOperateName());
                mememberGrowthRule.setDeleteFlag(0);
                mememberGrowthRule.setDeptId(param.getAdminOperate().getDeptId());
                mememberGrowthRule.setObtainMode(param.getObtainMode());
                mememberGrowthRule.setRuleName(param.getRuleName());
                mememberGrowthRule.setStatus(1);
                mememberGrowthRule.setWhetherPic(param.getWhetherPic());
                mememberGrowthRule.setDeptType(Integer.valueOf(param.getAdminOperate().getDeptType().getCode()));
                mememberGrowthRule.setGrowthNum("CZ" + formatdate + num + param.getAdminOperate().getDeptId());
                long flag = mememberGrowthRuleMapper.insert(mememberGrowthRule);
                if (flag != 0) {
                    result.setData(flag);
                    result.setCode("0000");
                } else {
                    result.setCode("9999");
                    result.setMsg("保存规则主表失败");
                    return result;
                }
            } else {
                result.setCode("8888");
                result.setMsg("请先设置【等级适用范围】，之后按照权限进行操作");
                return result;
            }
        }


        //插入注册-分享-点赞-晒单-收藏赠送表
        if (GetTypeEnum.REGISTER_HANDLESEL.getCode().equals(param.getObtainMode()) || GetTypeEnum.SHARE_HANDLESEL.getCode().equals(param.getObtainMode())
                || GetTypeEnum.LIKE_HANDLESEL.getCode().equals(param.getObtainMode())
                || GetTypeEnum.SHOW_ORDER_HANDLESEL.getCode().equals(param.getObtainMode())
                || GetTypeEnum.COLLECT_HANDLESEL.getCode().equals(param.getObtainMode())) {

            MememberGrowthAction mememberGrowthAction = new MememberGrowthAction();
            mememberGrowthAction.setIncrementId(mememberGrowthRule.getId());
            mememberGrowthAction.setObtainMode(param.getObtainMode());
            mememberGrowthAction.setGiveGrowthVal(Long.valueOf(param.getGiveGrowthVal()));
            if (param.getGiveGrowthMaxNum() != null) {
                mememberGrowthAction.setGiveGrowthMaxNum(Long.valueOf(param.getGiveGrowthMaxNum()));
            }
            mememberGrowthAction.setCreateTime(new Date());
            mememberGrowthAction.setCreator(param.getAdminOperate().getOperator());
            mememberGrowthAction.setCreatorName(param.getAdminOperate().getOperateName());
            mememberGrowthAction.setDeleteFlag(0);
            mememberGrowthAction.setDeptId(param.getAdminOperate().getDeptId());
            mememberGrowthAction.setStatus(1);
            long flags = mememberGrowthActionService.insert(mememberGrowthAction);
            if (flags == 0) {
                log.error("关联表插入失败");
                result.setCode("7777");
                return result;
            }
        } else if (GetTypeEnum.ORDER_HANDLESEL.getCode().equals(param.getObtainMode())) { //判断是否是下单赠送成长值
            MememberGrowthOrderRecharge mememberGrowthOrderRecharge = new MememberGrowthOrderRecharge();
            if (param.getConsumeType() != 3) { //如果是阶梯消费
                mememberGrowthOrderRecharge.setConsumeType(6);
            } else {
                mememberGrowthOrderRecharge.setConsumeType(3);
            }
            mememberGrowthOrderRecharge.setDeptId(param.getAdminOperate().getDeptId());
            mememberGrowthOrderRecharge.setCreateTime(new Date());
            mememberGrowthOrderRecharge.setCreatorName(param.getAdminOperate().getOperateName());
            mememberGrowthOrderRecharge.setCreator(param.getAdminOperate().getOperator());
            mememberGrowthOrderRecharge.setStatus(1);
            mememberGrowthOrderRecharge.setDeleteFlag(0);
            mememberGrowthOrderRecharge.setObtainMode(String.valueOf(1));
            mememberGrowthOrderRecharge.setGrowthRuleId(mememberGrowthRule.getId());
            if (param.getGiveGrowthMaxNum() != null) {
                mememberGrowthOrderRecharge.setGiveGrowthMaxNum(Long.valueOf(param.getGiveGrowthMaxNum()));
            }
            long orederFlag = mememberGrowthOrderRechargeService.insert(mememberGrowthOrderRecharge);
            //如果关联表插入成功,插入递增表
            if (orederFlag != 0) {
                List<MemberGrowthOrderMoneyRequest> orderList = param.getOrderHandleSel();
                MememberGrowthOrderMoney mememberGrowthOrderMoney = new MememberGrowthOrderMoney();
                mememberGrowthOrderMoney.setCreator(param.getAdminOperate().getOperator());
                mememberGrowthOrderMoney.setCreatorName(param.getAdminOperate().getOperateName());
                mememberGrowthOrderMoney.setCreateTime(new Date());
                mememberGrowthOrderMoney.setObtainMode(1);
                mememberGrowthOrderMoney.setDeleteFlag(0);
                mememberGrowthOrderMoney.setOrderId(mememberGrowthOrderRecharge.getId());
                mememberGrowthOrderMoney.setGrowthRuleId(mememberGrowthRule.getId());
                mememberGrowthOrderMoney.setDeptId(param.getAdminOperate().getDeptId());
                mememberGrowthOrderMoney.setConsumeType(param.getConsumeType());
                mememberGrowthOrderMoney.setStatus(1);
                if (param.getCostType() != null) {
                    mememberGrowthOrderMoney.setCostType(param.getCostType());
                }
                if (param.getGiveGrowthMaxNum() != null) {
                    mememberGrowthOrderMoney.setGiveGrowthMaxNum(Long.valueOf(param.getGiveGrowthMaxNum()));
                }
                if (param.getConsumeType().equals(6)) { //判断是否需要循环插入
                    orderList.forEach(items -> {
                        mememberGrowthOrderMoney.setConsumeMoney(items.getConsumeMoney());
                        mememberGrowthOrderMoney.setGiveGrowthVal(items.getGiveGrowthVal());
                        mememberGrowthOrderMoneyService.insert(mememberGrowthOrderMoney);
                    });
                } else {
                    mememberGrowthOrderMoney.setConsumeMoney(param.getConsumeMoney());
                    mememberGrowthOrderMoney.setGiveGrowthVal(Long.valueOf(param.getGiveGrowthVal()));
                    mememberGrowthOrderMoneyService.insert(mememberGrowthOrderMoney);
                }
            }
        } else if (GetTypeEnum.PAY_HANDLESEL.getCode().equals(param.getObtainMode())) { //判断是否是充值赠送成长值
            MememberGrowthOrderRecharge mememberGrowthOrderRecharge = new MememberGrowthOrderRecharge();
            if (param.getCostType() != 3) { //如果是阶梯充值
                mememberGrowthOrderRecharge.setCostType(6);
            } else {
                mememberGrowthOrderRecharge.setCostType(3);
            }
            mememberGrowthOrderRecharge.setDeptId(param.getAdminOperate().getDeptId());
            mememberGrowthOrderRecharge.setCreateTime(new Date());
            mememberGrowthOrderRecharge.setCreatorName(param.getAdminOperate().getOperateName());
            mememberGrowthOrderRecharge.setCreator(param.getAdminOperate().getOperator());
            mememberGrowthOrderRecharge.setStatus(1);
            mememberGrowthOrderRecharge.setDeleteFlag(0);
            mememberGrowthOrderRecharge.setObtainMode(String.valueOf(4));
            mememberGrowthOrderRecharge.setGrowthRuleId(mememberGrowthRule.getId());
            if (param.getGiveGrowthMaxNum() != null) {
                mememberGrowthOrderRecharge.setGiveGrowthMaxNum(Long.valueOf(param.getGiveGrowthMaxNum()));
            }
            long orederFlag = mememberGrowthOrderRechargeService.insert(mememberGrowthOrderRecharge);
            //如果关联表插入成功,插入递增表
            if (orederFlag != 0) {
                List<MemberGrowthOrderMoneyRequest> orderList = param.getCostHandleSel();
                MememberGrowthOrderMoney mememberGrowthOrderMoney = new MememberGrowthOrderMoney();
                mememberGrowthOrderMoney.setCreator(param.getAdminOperate().getOperator());
                mememberGrowthOrderMoney.setCreatorName(param.getAdminOperate().getOperateName());
                mememberGrowthOrderMoney.setCreateTime(new Date());
                mememberGrowthOrderMoney.setObtainMode(4);
                mememberGrowthOrderMoney.setDeleteFlag(0);
                mememberGrowthOrderMoney.setOrderId(mememberGrowthOrderRecharge.getId());
                mememberGrowthOrderMoney.setGrowthRuleId(mememberGrowthRule.getId());
                mememberGrowthOrderMoney.setDeptId(param.getAdminOperate().getDeptId());
                mememberGrowthOrderMoney.setCostType(param.getCostType());
                mememberGrowthOrderMoney.setStatus(1);
                if (param.getConsumeType() != null) {
                    mememberGrowthOrderMoney.setConsumeType(param.getConsumeType());
                }
                if (param.getGiveGrowthMaxNum() != null) {
                    mememberGrowthOrderMoney.setGiveGrowthMaxNum(Long.valueOf(param.getGiveGrowthMaxNum()));
                }
                if (param.getCostType().equals(6)) { //判断是否需要循环插入
                    orderList.forEach(items -> {
                        mememberGrowthOrderMoney.setConsumeMoney(items.getConsumeMoney());
                        mememberGrowthOrderMoney.setGiveGrowthVal(items.getGiveGrowthVal());
                        mememberGrowthOrderMoneyService.insert(mememberGrowthOrderMoney);
                    });
                } else {
                    mememberGrowthOrderMoney.setConsumeMoney(param.getConsumeMoney());
                    mememberGrowthOrderMoney.setGiveGrowthVal(Long.valueOf(param.getGiveGrowthVal()));
                    mememberGrowthOrderMoneyService.insert(mememberGrowthOrderMoney);
                }
            }
        } else if (GetTypeEnum.EVALUATE_HANDLESEL.getCode().equals(param.getObtainMode())) {//如果是评价赠送
            if (param.getWhetherPic() == 1) { //如果带图
                MememberGrowthAppraisal mememberGrowthAppraisal = new MememberGrowthAppraisal();
                mememberGrowthAppraisal.setWhetherPic(1);
                mememberGrowthAppraisal.setCreator(param.getAdminOperate().getOperator());
                mememberGrowthAppraisal.setCreatorName(param.getAdminOperate().getOperateName());
                mememberGrowthAppraisal.setCreateTime(new Date());
                mememberGrowthAppraisal.setDeleteFlag(0);
                mememberGrowthAppraisal.setGrowthRuleId(mememberGrowthRule.getId());
                mememberGrowthAppraisal.setDeptId(param.getAdminOperate().getDeptId());
                mememberGrowthAppraisal.setGiveGrowthVal(param.getAppGiveGrowthVal());
                mememberGrowthAppraisal.setGiveGrowthMaxNum(param.getAppGiveGrowthMaxNum());
                mememberGrowthAppraisalService.insert(mememberGrowthAppraisal);
            } else if (param.getWhetherPic() == 2) { //如果不带图
                MememberGrowthAppraisal mememberGrowthAppraisal = new MememberGrowthAppraisal();
                mememberGrowthAppraisal.setWhetherPic(2);
                mememberGrowthAppraisal.setCreator(param.getAdminOperate().getOperator());
                mememberGrowthAppraisal.setCreatorName(param.getAdminOperate().getOperateName());
                mememberGrowthAppraisal.setCreateTime(new Date());
                mememberGrowthAppraisal.setDeleteFlag(0);
                mememberGrowthAppraisal.setGrowthRuleId(mememberGrowthRule.getId());
                mememberGrowthAppraisal.setDeptId(param.getAdminOperate().getDeptId());
                mememberGrowthAppraisal.setGiveGrowthVal(param.getAppGiveGrowthVals());
                mememberGrowthAppraisal.setGiveGrowthMaxNum(param.getAppGiveGrowthMaxNums());
                mememberGrowthAppraisalService.insert(mememberGrowthAppraisal);
            } else if (param.getWhetherPic() == 3) { //如果全选
                MememberGrowthAppraisal mememberGrowthAppraisal = new MememberGrowthAppraisal();
                MememberGrowthAppraisal mememberGrowthAppraisal1 = new MememberGrowthAppraisal();
                mememberGrowthAppraisal.setWhetherPic(3);
                mememberGrowthAppraisal.setCreator(param.getAdminOperate().getOperator());
                mememberGrowthAppraisal.setCreatorName(param.getAdminOperate().getOperateName());
                mememberGrowthAppraisal.setCreateTime(new Date());
                mememberGrowthAppraisal.setDeleteFlag(0);
                mememberGrowthAppraisal.setGrowthRuleId(mememberGrowthRule.getId());
                mememberGrowthAppraisal.setDeptId(param.getAdminOperate().getDeptId());
                mememberGrowthAppraisal.setGiveGrowthVal(param.getAppGiveGrowthVals());
                mememberGrowthAppraisal.setGiveGrowthMaxNum(param.getAppGiveGrowthMaxNums());
                BeanUtil.copy(mememberGrowthAppraisal, mememberGrowthAppraisal1);
                mememberGrowthAppraisal1.setGiveGrowthVal(param.getAppGiveGrowthVal());
                mememberGrowthAppraisal1.setGiveGrowthMaxNum(param.getAppGiveGrowthMaxNum());
                mememberGrowthAppraisalService.insert(mememberGrowthAppraisal);
                mememberGrowthAppraisalService.insert(mememberGrowthAppraisal1);
            }
        } else if (Integer.valueOf(param.getObtainMode()) > 8) {  //如果是自定义赠送 插入对应关联表

            MememberGrowthAction mememberGrowthAction = new MememberGrowthAction();
            mememberGrowthAction.setIncrementId(mememberGrowthRule.getId());
            mememberGrowthAction.setObtainMode(param.getObtainMode());
            mememberGrowthAction.setGiveGrowthVal(Long.valueOf(param.getGiveGrowthVal()));
            if (param.getGiveGrowthMaxNum() != null) {
                mememberGrowthAction.setGiveGrowthMaxNum(Long.valueOf(param.getGiveGrowthMaxNum()));
            }
            mememberGrowthAction.setCreateTime(new Date());
            mememberGrowthAction.setCreator(param.getAdminOperate().getOperator());
            mememberGrowthAction.setCreatorName(param.getAdminOperate().getOperateName());
            mememberGrowthAction.setDeleteFlag(0);
            mememberGrowthAction.setDeptId(param.getAdminOperate().getDeptId());
            mememberGrowthAction.setStatus(1);
            long flags = mememberGrowthActionService.insert(mememberGrowthAction);
            if (flags == 0) {
                log.error("关联表插入失败");
                result.setCode("7777");
                return result;
            }
        }

        return result;
    }

    @Override
    public long delete(Long id) {
        return mememberGrowthRuleMapper.delete(id);
    }

    @Override
    public RestResult<Long> update(MemberGrowthRuleRequest param) {

        RestResult result = new RestResult();
        MememberGrowthRule mememberGrowthRule = new MememberGrowthRule();
        mememberGrowthRule.setRuleName(param.getRuleName());
        mememberGrowthRule.setId(param.getId());
        mememberGrowthRule.setWhetherPic(param.getWhetherPic());
        //修改成长值规则主表
        long flag = mememberGrowthRuleMapper.update(mememberGrowthRule);
        if (flag != 0) {
            result.setData(flag);
            result.setCode("0000");
        } else {
            result.setCode("9999");
            result.setMsg("修改规则主表失败");
            return result;
        }
        /*
         * 根据规则类型判断修改哪种类型规则*/
        if (GetTypeEnum.REGISTER_HANDLESEL.getCode().equals(param.getObtainMode()) || GetTypeEnum.SHARE_HANDLESEL.getCode().equals(param.getObtainMode())
                || GetTypeEnum.LIKE_HANDLESEL.getCode().equals(param.getObtainMode())
                || GetTypeEnum.SHOW_ORDER_HANDLESEL.getCode().equals(param.getObtainMode())
                || GetTypeEnum.COLLECT_HANDLESEL.getCode().equals(param.getObtainMode()) || Integer.valueOf(param.getObtainMode()) > 8) {

            MememberGrowthAction mememberGrowthAction = new MememberGrowthAction();
            mememberGrowthAction.setIncrementId(param.getId());
            mememberGrowthAction.setGiveGrowthVal(Long.valueOf(param.getGiveGrowthVal()));
            if (param.getGiveGrowthMaxNum() != null) {
                mememberGrowthAction.setGiveGrowthMaxNum(Long.valueOf(param.getGiveGrowthMaxNum()));
            }
            mememberGrowthAction.setUpdateTime(new Date());
            mememberGrowthAction.setUpdater(param.getAdminOperate().getOperator());
            mememberGrowthAction.setUpdaterName(param.getAdminOperate().getOperateName());
            long flags = mememberGrowthActionService.update(mememberGrowthAction);
            if (flags == 0) {
                log.error("关联表修改失败");
                result.setCode("8888");
                return result;
            }
        } else if (GetTypeEnum.ORDER_HANDLESEL.getCode().equals(param.getObtainMode())) { //如果是下单编辑
            if (param.getConsumeType() != 6) { //如果是每消费
                MememberGrowthOrderMoney mememberGrowthOrderMoney = new MememberGrowthOrderMoney();
                //先修改中间表
                MememberGrowthOrderRecharge mememberGrowthOrderRecharge = new MememberGrowthOrderRecharge();
                mememberGrowthOrderRecharge.setConsumeType(3);
                mememberGrowthOrderRecharge.setGrowthRuleId(param.getId());
                mememberGrowthOrderRecharge.setUpdateTime(new Date());
                mememberGrowthOrderRecharge.setUpdater(param.getAdminOperate().getOperator());
                mememberGrowthOrderRecharge.setUpdaterName(param.getAdminOperate().getOperateName());
                if (param.getGiveGrowthMaxNum() != null) {
                    mememberGrowthOrderRecharge.setGiveGrowthMaxNum(Long.valueOf(param.getGiveGrowthMaxNum()));
                }
                mememberGrowthOrderRechargeService.update(mememberGrowthOrderRecharge);
                //再修改关联表
                MemberGrowthOrderResponse memberGrowthOrderResponse = new MemberGrowthOrderResponse();
                memberGrowthOrderResponse = mememberGrowthOrderRechargeService.queryByPrimaryKey(param.getId());
                //先获取中间表id再删除
                mememberGrowthOrderMoneyService.deleteByOrderId(memberGrowthOrderResponse.getId());
                //再插入修改的值
                mememberGrowthOrderMoney.setOrderId(memberGrowthOrderResponse.getId());
                mememberGrowthOrderMoney.setCreator(param.getAdminOperate().getOperator());
                mememberGrowthOrderMoney.setCreatorName(param.getAdminOperate().getOperateName());
                mememberGrowthOrderMoney.setCreateTime(new Date());
                mememberGrowthOrderMoney.setObtainMode(1);
                mememberGrowthOrderMoney.setDeleteFlag(0);
                mememberGrowthOrderMoney.setGrowthRuleId(param.getId());
                mememberGrowthOrderMoney.setDeptId(param.getAdminOperate().getDeptId());
                mememberGrowthOrderMoney.setConsumeType(3);
                mememberGrowthOrderMoney.setStatus(1);
                mememberGrowthOrderMoney.setConsumeMoney(param.getConsumeMoney());
                mememberGrowthOrderMoney.setGiveGrowthVal(Long.valueOf(param.getGiveGrowthVal()));
                if (param.getCostType() != null) {
                    mememberGrowthOrderMoney.setCostType(param.getCostType());
                }
                if (param.getGiveGrowthMaxNum() != null) {
                    mememberGrowthOrderMoney.setGiveGrowthMaxNum(Long.valueOf(param.getGiveGrowthMaxNum()));
                }
                mememberGrowthOrderMoneyService.insert(mememberGrowthOrderMoney);
            } else { // 如果是阶梯消费
                MememberGrowthOrderMoney mememberGrowthOrderMoney = new MememberGrowthOrderMoney();
                //先修改中间表
                MememberGrowthOrderRecharge mememberGrowthOrderRecharge = new MememberGrowthOrderRecharge();
                mememberGrowthOrderRecharge.setConsumeType(6);
                mememberGrowthOrderRecharge.setGrowthRuleId(param.getId());
                mememberGrowthOrderRecharge.setUpdateTime(new Date());
                mememberGrowthOrderRecharge.setUpdater(param.getAdminOperate().getOperator());
                mememberGrowthOrderRecharge.setUpdaterName(param.getAdminOperate().getOperateName());
                if (param.getGiveGrowthMaxNum() != null) {
                    mememberGrowthOrderRecharge.setGiveGrowthMaxNum(Long.valueOf(param.getGiveGrowthMaxNum()));
                }
                mememberGrowthOrderRechargeService.update(mememberGrowthOrderRecharge);
                //再修改关联表
                MemberGrowthOrderResponse memberGrowthOrderResponse = mememberGrowthOrderRechargeService.queryByPrimaryKey(param.getId());
                //先获取中间表id再删除
                mememberGrowthOrderMoneyService.deleteByOrderId(memberGrowthOrderResponse.getId());
                //再插入修改的值
                mememberGrowthOrderMoney.setOrderId(memberGrowthOrderResponse.getId());
                mememberGrowthOrderMoney.setCreator(param.getAdminOperate().getOperator());
                mememberGrowthOrderMoney.setCreatorName(param.getAdminOperate().getOperateName());
                mememberGrowthOrderMoney.setCreateTime(new Date());
                mememberGrowthOrderMoney.setObtainMode(1);
                mememberGrowthOrderMoney.setDeleteFlag(0);
                mememberGrowthOrderMoney.setGrowthRuleId(param.getId());
                mememberGrowthOrderMoney.setDeptId(param.getAdminOperate().getDeptId());
                mememberGrowthOrderMoney.setConsumeType(6);
                mememberGrowthOrderMoney.setStatus(1);
                if (param.getCostType() != null) {
                    mememberGrowthOrderMoney.setCostType(param.getCostType());
                }
                if (param.getGiveGrowthMaxNum() != null) {
                    mememberGrowthOrderMoney.setGiveGrowthMaxNum(Long.valueOf(param.getGiveGrowthMaxNum()));
                }
                List<MemberGrowthOrderMoneyRequest> orderMoneyRequestList = param.getOrderHandleSel();
                if (param.getOrderHandleSel().size() != 0) {

                    orderMoneyRequestList.forEach(items -> {
                        mememberGrowthOrderMoney.setConsumeMoney(items.getConsumeMoney());
                        mememberGrowthOrderMoney.setGiveGrowthVal(items.getGiveGrowthVal());
                        mememberGrowthOrderMoneyService.insert(mememberGrowthOrderMoney);
                    });
                } else {
                    log.error("数组为空，无法修改");
                    result.setCode("9999");
                    return result;
                }
            }
        } else if (GetTypeEnum.PAY_HANDLESEL.getCode().equals(param.getObtainMode())) {//如果是充值赠送
            if (param.getCostType() != 6) { //如果是每充值
                MememberGrowthOrderMoney mememberGrowthOrderMoney = new MememberGrowthOrderMoney();
                //先修改中间表
                MememberGrowthOrderRecharge mememberGrowthOrderRecharge = new MememberGrowthOrderRecharge();
                mememberGrowthOrderRecharge.setCostType(3);
                mememberGrowthOrderRecharge.setGrowthRuleId(param.getId());
                mememberGrowthOrderRecharge.setUpdateTime(new Date());
                mememberGrowthOrderRecharge.setUpdater(param.getAdminOperate().getOperator());
                mememberGrowthOrderRecharge.setUpdaterName(param.getAdminOperate().getOperateName());
                if (param.getGiveGrowthMaxNum() != null) {
                    mememberGrowthOrderRecharge.setGiveGrowthMaxNum(Long.valueOf(param.getGiveGrowthMaxNum()));
                }
                mememberGrowthOrderRechargeService.update(mememberGrowthOrderRecharge);
                //再修改关联表
                MemberGrowthOrderResponse memberGrowthOrderResponse = mememberGrowthOrderRechargeService.queryByPrimaryKey(param.getId());
                //先获取中间表id再删除
                mememberGrowthOrderMoneyService.deleteByOrderId(memberGrowthOrderResponse.getId());
                //再插入修改的值
                mememberGrowthOrderMoney.setOrderId(memberGrowthOrderResponse.getId());
                mememberGrowthOrderMoney.setCreator(param.getAdminOperate().getOperator());
                mememberGrowthOrderMoney.setCreatorName(param.getAdminOperate().getOperateName());
                mememberGrowthOrderMoney.setCreateTime(new Date());
                mememberGrowthOrderMoney.setObtainMode(4);
                mememberGrowthOrderMoney.setDeleteFlag(0);
                mememberGrowthOrderMoney.setGrowthRuleId(param.getId());
                mememberGrowthOrderMoney.setDeptId(param.getAdminOperate().getDeptId());
                mememberGrowthOrderMoney.setCostType(3);
                mememberGrowthOrderMoney.setStatus(1);
                mememberGrowthOrderMoney.setConsumeMoney(param.getConsumeMoney());
                mememberGrowthOrderMoney.setGiveGrowthVal(Long.valueOf(param.getGiveGrowthVal()));
                if (param.getCostType() != null) {
                    mememberGrowthOrderMoney.setCostType(param.getCostType());
                }
                if (param.getGiveGrowthMaxNum() != null) {
                    mememberGrowthOrderMoney.setGiveGrowthMaxNum(Long.valueOf(param.getGiveGrowthMaxNum()));
                }
                mememberGrowthOrderMoneyService.insert(mememberGrowthOrderMoney);
            } else { // 如果是阶梯充值
                MememberGrowthOrderMoney mememberGrowthOrderMoney = new MememberGrowthOrderMoney();
                //先修改中间表
                MememberGrowthOrderRecharge mememberGrowthOrderRecharge = new MememberGrowthOrderRecharge();
                mememberGrowthOrderRecharge.setCostType(6);
                mememberGrowthOrderRecharge.setGrowthRuleId(param.getId());
                mememberGrowthOrderRecharge.setUpdateTime(new Date());
                mememberGrowthOrderRecharge.setUpdater(param.getAdminOperate().getOperator());
                mememberGrowthOrderRecharge.setUpdaterName(param.getAdminOperate().getOperateName());
                if (param.getGiveGrowthMaxNum() != null) {
                    mememberGrowthOrderRecharge.setGiveGrowthMaxNum(Long.valueOf(param.getGiveGrowthMaxNum()));
                }
                mememberGrowthOrderRechargeService.update(mememberGrowthOrderRecharge);
                //再修改关联表
                MemberGrowthOrderResponse memberGrowthOrderResponse = mememberGrowthOrderRechargeService.queryByPrimaryKey(param.getId());
                //先获取中间表id再删除
                mememberGrowthOrderMoneyService.deleteByOrderId(memberGrowthOrderResponse.getId());
                //再插入修改的值
                mememberGrowthOrderMoney.setOrderId(memberGrowthOrderResponse.getId());
                mememberGrowthOrderMoney.setCreator(param.getAdminOperate().getOperator());
                mememberGrowthOrderMoney.setCreatorName(param.getAdminOperate().getOperateName());
                mememberGrowthOrderMoney.setCreateTime(new Date());
                mememberGrowthOrderMoney.setObtainMode(4);
                mememberGrowthOrderMoney.setDeleteFlag(0);
                mememberGrowthOrderMoney.setGrowthRuleId(param.getId());
                mememberGrowthOrderMoney.setDeptId(param.getAdminOperate().getDeptId());
                mememberGrowthOrderMoney.setCostType(6);
                mememberGrowthOrderMoney.setStatus(1);
                if (param.getCostType() != null) {
                    mememberGrowthOrderMoney.setCostType(param.getCostType());
                }
                if (param.getGiveGrowthMaxNum() != null) {
                    mememberGrowthOrderMoney.setGiveGrowthMaxNum(Long.valueOf(param.getGiveGrowthMaxNum()));
                }
                List<MemberGrowthOrderMoneyRequest> orderMoneyRequestList = param.getCostHandleSel();
                if (param.getCostHandleSel().size() != 0) {

                    orderMoneyRequestList.forEach(items -> {
                        mememberGrowthOrderMoney.setConsumeMoney(items.getConsumeMoney());
                        mememberGrowthOrderMoney.setGiveGrowthVal(items.getGiveGrowthVal());
                        mememberGrowthOrderMoneyService.insert(mememberGrowthOrderMoney);
                    });
                } else {
                    log.error("数组为空，无法修改");
                    result.setCode("9999");
                    return result;
                }
            }
        } else if (GetTypeEnum.EVALUATE_HANDLESEL.getCode().equals(param.getObtainMode())) { //如果是评价赠送
            //先清除原先表的数据
            mememberGrowthAppraisalService.deleteByRuleId(param.getId());
            if (param.getWhetherPic() == 1) { //如果选择带图
                //插入对应信息
                MememberGrowthAppraisal mememberGrowthAppraisal = new MememberGrowthAppraisal();
                mememberGrowthAppraisal.setWhetherPic(1);
                mememberGrowthAppraisal.setCreator(param.getAdminOperate().getOperator());
                mememberGrowthAppraisal.setCreatorName(param.getAdminOperate().getOperateName());
                mememberGrowthAppraisal.setCreateTime(new Date());
                mememberGrowthAppraisal.setGrowthRuleId(param.getId());
                mememberGrowthAppraisal.setDeptId(param.getAdminOperate().getDeptId());
                mememberGrowthAppraisal.setGiveGrowthVal(param.getAppGiveGrowthVal());
                mememberGrowthAppraisal.setGiveGrowthMaxNum(param.getAppGiveGrowthMaxNum());
                mememberGrowthAppraisalService.insert(mememberGrowthAppraisal);
                result.setCode("0000");
            } else if (param.getWhetherPic() == 2) { //如果选择不带图
                //插入对应信息
                MememberGrowthAppraisal mememberGrowthAppraisal = new MememberGrowthAppraisal();
                mememberGrowthAppraisal.setWhetherPic(2);
                mememberGrowthAppraisal.setCreator(param.getAdminOperate().getOperator());
                mememberGrowthAppraisal.setCreatorName(param.getAdminOperate().getOperateName());
                mememberGrowthAppraisal.setCreateTime(new Date());
                mememberGrowthAppraisal.setGrowthRuleId(param.getId());
                mememberGrowthAppraisal.setDeptId(param.getAdminOperate().getDeptId());
                mememberGrowthAppraisal.setGiveGrowthVal(param.getAppGiveGrowthVals());
                mememberGrowthAppraisal.setGiveGrowthMaxNum(param.getAppGiveGrowthMaxNums());
                mememberGrowthAppraisalService.insert(mememberGrowthAppraisal);
            } else if (param.getWhetherPic() == 3) { //如果全选
                MememberGrowthAppraisal mememberGrowthAppraisal = new MememberGrowthAppraisal();
                MememberGrowthAppraisal mememberGrowthAppraisal1 = new MememberGrowthAppraisal();
                mememberGrowthAppraisal.setWhetherPic(3);
                mememberGrowthAppraisal.setCreator(param.getAdminOperate().getOperator());
                mememberGrowthAppraisal.setCreatorName(param.getAdminOperate().getOperateName());
                mememberGrowthAppraisal.setCreateTime(new Date());
                mememberGrowthAppraisal.setDeleteFlag(0);
                mememberGrowthAppraisal.setGrowthRuleId(mememberGrowthRule.getId());
                mememberGrowthAppraisal.setDeptId(param.getAdminOperate().getDeptId());
                mememberGrowthAppraisal.setGiveGrowthVal(param.getAppGiveGrowthVals());
                mememberGrowthAppraisal.setGiveGrowthMaxNum(param.getAppGiveGrowthMaxNums());
                BeanUtil.copy(mememberGrowthAppraisal, mememberGrowthAppraisal1);
                mememberGrowthAppraisal1.setGiveGrowthVal(param.getAppGiveGrowthVal());
                mememberGrowthAppraisal1.setGiveGrowthMaxNum(param.getAppGiveGrowthMaxNum());
                mememberGrowthAppraisalService.insert(mememberGrowthAppraisal);
                mememberGrowthAppraisalService.insert(mememberGrowthAppraisal1);
                result.setCode("0000");
            }
        }
        result.setData(flag);
        return result;
    }

    @Override
    public MememberGrowthRule queryByPrimaryKey(Long id) {
        return mememberGrowthRuleMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<MememberGrowthRule> query(MememberGrowthRule mememberGrowthRule) {
        return mememberGrowthRuleMapper.query(mememberGrowthRule);
    }
    @Override
    public RestResult<PageResult<MemberGrowthRuleResponse>> queryPage(MemberGrowthRuleRequest memberGrowthRuleRequest) {
        /*判断当前登录账号的所属层级，如果是子，母平台，则直接看到所属平台列表信息*/
        Integer deptType = memberGrowthRuleRequest.getAdminOperate().getDeptType().getCode();
        if (deptType == 0 || deptType == 1) {
            Long deptId = memberGrowthRuleRequest.getAdminOperate().getDeptId();
            memberGrowthRuleRequest.setDeptId(deptId);
        }
        //计算下标
        int startIndex = (memberGrowthRuleRequest.getStart() - 1) * memberGrowthRuleRequest.getLimit();
        List<MememberGrowthRule> list = mememberGrowthRuleMapper.queryPage(startIndex, memberGrowthRuleRequest.getLimit(), memberGrowthRuleRequest);
        list.forEach(items -> {
            RestResult<String> deptName = sysDeptApi.queryDeptNameById(items.getDeptId());
            items.setDeptName(deptName.getData());
        });
        //根据赠送方式获取成长值
        list.forEach(items -> {
            String obtainMode = items.getObtainMode();
            if (obtainMode.equals("1") || obtainMode.equals("4")) {
                MememberGrowthOrderMoney mememberGrowthOrderMoney = mememberGrowthOrderMoneyService.getGiveGrowthValById(items.getId());
                items.setGiveGrowthVal(mememberGrowthOrderMoney.getGiveGrowthVal());
            } else if (obtainMode.equals("2") || obtainMode.equals("5") || obtainMode.equals("6") || obtainMode.equals("7") || obtainMode.equals("8") || Integer.valueOf(obtainMode) > 8) {
                MememberGrowthAction mememberGrowthAction = mememberGrowthActionService.getGiveGrowthValById(items.getId());
                items.setGiveGrowthVal(mememberGrowthAction.getGiveGrowthVal());
            } else if (obtainMode.equals("3")) {
                MememberGrowthAppraisal mememberGrowthAppraisal = mememberGrowthAppraisalService.getGiveGrowthValById(items.getId());
                items.setGiveGrowthVal(Long.valueOf(String.valueOf(mememberGrowthAppraisal.getGiveGrowthVal())));
            }
            if (obtainMode.equals("1")) {
                items.setRuleType("下单赠送");
            } else if (obtainMode.equals("2")) {
                items.setRuleType("注册赠送");
            } else if (obtainMode.equals("3")) {
                items.setRuleType("评价赠送");
            } else if (obtainMode.equals("4")) {
                items.setRuleType("充值赠送");
            } else if (obtainMode.equals("5")) {
                items.setRuleType("分享赠送");
            } else if (obtainMode.equals("6")) {
                items.setRuleType("点赞赠送");
            } else if (obtainMode.equals("7")) {
                items.setRuleType("晒单赠送");
            } else if (obtainMode.equals("8")) {
                items.setRuleType("收藏赠送");
            }  else if (obtainMode.equals("9")) {
                items.setRuleType("参与平台活动");
            } else if (obtainMode.equals("10")) {
                items.setRuleType("发布活动");
            }
        });
        long count = mememberGrowthRuleMapper.queryPageCount(memberGrowthRuleRequest);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        RestResult result = new RestResult();
        result.setData(pageResult);
        result.setCode("0000");
        return result;
    }

    @Override
    public RestResult<Long> delByRuleId(MemberGrowthRuleRequest mememberGrowthRule) {
        RestResult result = new RestResult();
        //先删除主表对应信息
        long flag = mememberGrowthRuleMapper.delByRuleId(mememberGrowthRule.getId());
        //再判断属于哪种活动类型
        //插入注册-分享-点赞-晒单-收藏赠送表
        if (GetTypeEnum.REGISTER_HANDLESEL.getCode().equals(mememberGrowthRule.getObtainMode()) || GetTypeEnum.SHARE_HANDLESEL.getCode().equals(mememberGrowthRule.getObtainMode())
                || GetTypeEnum.LIKE_HANDLESEL.getCode().equals(mememberGrowthRule.getObtainMode())
                || GetTypeEnum.SHOW_ORDER_HANDLESEL.getCode().equals(mememberGrowthRule.getObtainMode())
                || GetTypeEnum.COLLECT_HANDLESEL.getCode().equals(mememberGrowthRule.getObtainMode()) || Integer.valueOf(mememberGrowthRule.getObtainMode()) > 8) {
            if (flag != 0) { //主表删除成功后再删除关联表信息
                long acFlag = mememberGrowthActionService.delete(mememberGrowthRule.getId());
                if (acFlag != 0) {
                    result.setCode("0000");
                } else {
                    result.setCode("9999");
                    return result;
                }
            }
        } else if (GetTypeEnum.ORDER_HANDLESEL.getCode().equals(mememberGrowthRule.getObtainMode()) || GetTypeEnum.PAY_HANDLESEL.getCode().equals(mememberGrowthRule.getObtainMode())) { //如果是下单或充值赠送
            //先删除中间表
            long rechargeFlag = mememberGrowthOrderRechargeService.delete(mememberGrowthRule.getId());
            if (rechargeFlag != 0) { //如果中间表删除成功 再根据中间表id删除对应关联表信息
                MemberGrowthOrderResponse memberGrowthOrderResponse = mememberGrowthOrderRechargeService.queryOrderId(mememberGrowthRule.getId());
                long orderFlag = mememberGrowthOrderMoneyService.deleteByOrderId(memberGrowthOrderResponse.getId());
                if (orderFlag != 0) {
                    result.setCode("0000");
                } else {
                    result.setCode("9999");
                }

            } else {
                result.setCode("9999");
                return result;
            }
        } else if (GetTypeEnum.EVALUATE_HANDLESEL.getCode().equals(mememberGrowthRule.getObtainMode())) {//如果是评价赠送
            long appFlag = mememberGrowthAppraisalService.deleteByRuleId(mememberGrowthRule.getId());
            if (appFlag != 0) {
                result.setCode("0000");
            } else {
                result.setCode("9999");
                return result;
            }
        }


        result.setData(flag);
        return result;
    }

    @Override
    public RestResult<Long> updateGrowthRuleStatus(MemberGrowthRuleRequest request) {
        RestResult result = new RestResult();
        MememberGrowthRule mememberGrowthRule = new MememberGrowthRule();
        //判断是启用还是停用
        if (request.getStatus() == 0) { //如果是启用
            mememberGrowthRule.setObtainMode(request.getObtainMode());
            mememberGrowthRule.setStatus(0);
            mememberGrowthRule.setDeleteFlag(0);
            mememberGrowthRule.setDeptId(request.getAdminOperate().getDeptId());
            List<MememberGrowthRule> resultGrowth = mememberGrowthRuleMapper.query(mememberGrowthRule);//判断是否有其他同类型规则被启用
            if (Integer.valueOf(request.getObtainMode()) < 9) {  //判断是否是自定义规则类型
                if (CollectionUtils.isNotEmpty(resultGrowth)) {
                    result.setCode("5555");
                    result.setMsg("同类型规则只能启用一次，请先停用其他同类型规则");
                } else {
                    mememberGrowthRule.setId(request.getId());
                    long flag = mememberGrowthRuleMapper.updateGrowthRuleStatus(mememberGrowthRule);
                    result.setCode("0000");
                    result.setData(flag);
                }
            }

        } else {  //如果是停用

            mememberGrowthRule.setStatus(1);
            mememberGrowthRule.setId(request.getId());
            long flag = mememberGrowthRuleMapper.updateGrowthRuleStatus(mememberGrowthRule);
            result.setCode("0000");
            result.setData(flag);
        }

        return result;
    }

    @Override
    public RestResult<PageResult<MemberGrowthRuleResponse>> queryGrowthRule(MemberGrowthRuleRequest request) {
        if (request.getMsName() != null) {
            List<Long> list = sysDeptApi.querymMerchantOrStoreListByName(request.getMsName());
            request.setMsDeptIds(list);
        }
        /*先判断当前登录账号*/
        Integer deptType = request.getAdminOperate().getDeptType().getCode();
        if (deptType == 0) {
            request.setDeptType(6L);
            request.setPlatformId(request.getChildId());
        } else if (deptType == 1) {
            request.setChildId(request.getAdminOperate().getBusinessId());
            request.setDeptType(1L);
        }
        // 计算下标
        int startIndex = (request.getStart() - 1) * request.getLimit();
        List<MemberGrowthRuleResponse> list = mememberGrowthRuleMapper.queryGrowthRule(startIndex, request.getLimit(), request);
        long count = mememberGrowthRuleMapper.queryRuleCount(request);
        list.forEach(items -> {
            RestResult<String> name = sysDeptApi.queryDeptNameById(Long.valueOf(items.getDeptId()));
            ChildNameRequest query = new ChildNameRequest();
            query.setDeptType(Long.valueOf(items.getDeptType()));
            query.setDeptId(Long.valueOf(items.getDeptId()));
            RestResult<String> names = sysDeptApi.queryChildNameByDeptId(query);
            items.setDeptName(name.getData());
            items.setChildName(names.getData());
        });

        //根据赠送方式获取成长值
        list.forEach(items -> {
            String obtainMode = items.getObtainMode();
            if (obtainMode.equals("1") || obtainMode.equals("4")) {
                MememberGrowthOrderMoney mememberGrowthOrderMoney = mememberGrowthOrderMoneyService.getGiveGrowthValById(items.getId());
                items.setGiveGrowthVal(mememberGrowthOrderMoney.getGiveGrowthVal());
            } else if (obtainMode.equals("2") || obtainMode.equals("5") || obtainMode.equals("6") || obtainMode.equals("7") || obtainMode.equals("8") || Long.valueOf(obtainMode) > 8) {
                MememberGrowthAction mememberGrowthAction = mememberGrowthActionService.getGiveGrowthValById(items.getId());
                items.setGiveGrowthVal(mememberGrowthAction.getGiveGrowthVal());
            } else if (obtainMode.equals("3")) {
                MememberGrowthAppraisal mememberGrowthAppraisal = mememberGrowthAppraisalService.getGiveGrowthValById(items.getId());
                items.setGiveGrowthVal(Long.valueOf(String.valueOf(mememberGrowthAppraisal.getGiveGrowthVal())));
            }
        });

        //根据赠送方式获取规则类型
        list.forEach(items -> {
            String obtainMode = items.getObtainMode();
            if (obtainMode.equals("1")) {
                items.setRuleType("下单赠送");
            } else if (obtainMode.equals("2")) {
                items.setRuleType("注册赠送");
            } else if (obtainMode.equals("3")) {
                items.setRuleType("评价赠送");
            } else if (obtainMode.equals("4")) {
                items.setRuleType("充值赠送");
            } else if (obtainMode.equals("5")) {
                items.setRuleType("分享赠送");
            } else if (obtainMode.equals("6")) {
                items.setRuleType("点赞赠送");
            } else if (obtainMode.equals("7")) {
                items.setRuleType("晒单赠送");
            } else if (obtainMode.equals("8")) {
                items.setRuleType("收藏赠送");
            }
        });

        PageResult pageResult = new PageResult();
        pageResult.setTotal(count);
        pageResult.setRows(list);
        RestResult result = new RestResult();
        result.setData(pageResult);
        result.setCode("0000");
        return result;
    }

    @Override
    public RestResult<String> getDeptNameByDeptId(MemberGrowthRuleRequest request) {
        return sysDeptApi.queryDeptNameById(Long.valueOf(request.getAdminOperate().getDeptId()));
    }

    @Override
    /*根据规则主id查询查询对应操作人的所属层级及对应平台*/
    public RestResult<MemberGrowthRuleResponse> getDeptInfoById(Long id) {
        RestResult result = new RestResult();
        MemberGrowthRuleResponse memberGrowthRuleResponse = mememberGrowthRuleMapper.getDeptInfoById(id);
        RestResult<String> deptName = sysDeptApi.queryDeptNameById(memberGrowthRuleResponse.getDeptId());
        memberGrowthRuleResponse.setDeptName(deptName.getData());
        result.setData(memberGrowthRuleResponse);
        return result;
    }

    /*根据子平台id查询成长值规则列表*/
    public RestResult<PageResult<MemberGrowthRuleResponse>> getRulistByChild(MemberGrowthRuleRequest request) {

        request.setDeptType(1L);
        request.setChildId(request.getAdminOperate().getChildIds().get(0));
        // 计算下标
        int startIndex = (request.getStart() - 1) * request.getLimit();
        List<MemberGrowthRuleResponse> list = mememberGrowthRuleMapper.getRulistByChild(startIndex, request.getLimit(), request);
        long count = mememberGrowthRuleMapper.getRulistByChildCount(request);
        list.forEach(items -> {
            RestResult<String> name = sysDeptApi.queryDeptNameById(Long.valueOf(items.getDeptId()));
            items.setDeptName(name.getData());
        });
        //根据赠送方式获取成长值
        list.forEach(items -> {
            String obtainMode = items.getObtainMode();
            if (obtainMode.equals("1") || obtainMode.equals("4")) {
                MememberGrowthOrderMoney mememberGrowthOrderMoney = mememberGrowthOrderMoneyService.getGiveGrowthValById(items.getId());
                if (null != mememberGrowthOrderMoney){
                    items.setGiveGrowthVal(mememberGrowthOrderMoney.getGiveGrowthVal());
                }
            } else if (obtainMode.equals("2") || obtainMode.equals("5") || obtainMode.equals("6") || obtainMode.equals("7") || obtainMode.equals("8")) {
                MememberGrowthAction mememberGrowthAction = mememberGrowthActionService.getGiveGrowthValById(items.getId());
                if (null != mememberGrowthAction){
                    items.setGiveGrowthVal(mememberGrowthAction.getGiveGrowthVal());
                }
            } else if (obtainMode.equals("3")) {
                MememberGrowthAppraisal mememberGrowthAppraisal = mememberGrowthAppraisalService.getGiveGrowthValById(items.getId());
                if (null != mememberGrowthAppraisal.getGiveGrowthVal()){
                    items.setGiveGrowthVal(mememberGrowthAppraisal.getGiveGrowthVal().longValue());
                }
            }
        });
        PageResult pageResult = new PageResult();
        pageResult.setTotal(count);
        pageResult.setRows(list);
        RestResult result = new RestResult();
        result.setData(pageResult);
        result.setCode("0000");
        return result;
    }


    /**
     * 获取会员[成长值]详情
     *
     * @param request 入参
     * @return 返回单个成长值对象
     * @date 2020-5-26 16:53:10
     * @author tzc
     */
    @Override
    public RestResult<MemberGrowthActionApiResponse> getMemberGrowthInfo(MemberGrowthActionApiRequest request) {
        RestResult restResult = new RestResult();
        log.info("query request info begin =========》,{}", request);
        log.info("开始查询会员成长值信息===》入参,{}",request);

        if (request.getMemberId() ==null) {
            log.info("查询会员成长值==》【memberId】为空,{}",request.getMemberId());
            return RestResult.error(GlobalConsts.ERROR_CODE_PARAM_MISSING, "第三方用户Id为空");
        }
        Long thirdUserId = request.getMemberId();//第三方ID
        try {
            //查询会员主表
            log.info("查询会员成长值信息==》入参：第三方用户id:thirdUserId,{}",thirdUserId);
            MememberUser mememberUserInfoVal = mememberUserMapper.queryByPrimaryKey(thirdUserId);
            MemberGrowthActionApiResponse memberResponse =new MemberGrowthActionApiResponse();
            if (null != mememberUserInfoVal) {
                log.info("query mememberIntegral info is:=========>,{}", mememberUserInfoVal);
                log.info("查询会员成长值信息不为空===》结果,{}",JSON.toJSONString(mememberUserInfoVal));

                memberResponse.setGiveGrowthVal(mememberUserInfoVal.getGrowthNumber());
                memberResponse.setThirdUserId(thirdUserId+"");
                restResult.setCode(EXCEPTION.SUCCESS.getName());
                restResult.setMsg(EXCEPTION.SUCCESS.getDesc());
                restResult.setData(memberResponse);
            } else {
                log.info("查询会员成长值信息为空===》结果,{}",mememberUserInfoVal);
                restResult.setCode(EXCEPTION.ISNULL.getName());
                restResult.setMsg(EXCEPTION.ISNULL.getDesc());
            }
            return restResult;
        } catch (Exception e) {
            log.info("查询会员成长值信息==》根据第三方id【thirdUserId】查询会员主表==》异常,{}",e.getMessage());
            log.info("Exception info is:=========>,{}", e.getMessage());
        }
        return restResult;
    }

    /**
     * 获取会员[成长值]详情
     *
     * @param request 入参
     * @return 返回单个成长值对象
     * @date 2020-5-26 16:53:10
     * @author tzc
     */
    @Override
    public RestResult<MemberGrowthActionApiResponse> updateMemberGrowthInfo(MemberGrowthActionApiRequest request) {
        RestResult restResult = new RestResult();
        log.info("[修改成长值]MememberGrowthRuleServiceImpl.updateMemberGrowthInfo(), request info begin =========》,{}", request);

        if (request.getMemberId() ==null) {
            return RestResult.error(GlobalConsts.ERROR_CODE_PARAM_MISSING, "用户Id为空");
        }
        if (null != request.getDeptType()) {
            return RestResult.error(GlobalConsts.ERROR_CODE_PARAM_MISSING, "所属组织层级为空");
        }
        if (null != request.getDeptId()) {
            return RestResult.error(GlobalConsts.ERROR_CODE_PARAM_MISSING, "所属部门Id为空");
        }
        if (null != request.getGiveGrowthVal()) {
            return RestResult.error(GlobalConsts.ERROR_CODE_PARAM_MISSING, "所属部门Id为空");
        }

        final Long thirdUserId = request.getMemberId();
        //所属组织层级
        final Integer deptType = request.getDeptType();
        //部门编号
        final Long deptId = request.getDeptId();
        //成长值
        Long growthVal = request.getGiveGrowthVal();
        try {
            //查询会员主表
            MememberUser mememberAction = mememberUserMapper.queryByPrimaryKey(thirdUserId);
            if (null != mememberAction) {
                MememberGrowthBase mememberGrowthBase = new MememberGrowthBase();
                mememberGrowthBase.setDeptType(deptType);
                mememberGrowthBase.setDeptId(deptId.intValue());
                mememberGrowthBase.setDeleteFlag(0);
                List<MememberGrowthBase> list = mememberGrowthBaseMapper.listMemberGrowthBaseInfoByDeptInfo(mememberGrowthBase);
                if (null == list || list.size() != 1) {
                    log.debug("[修改成长值]查询 mem_member_growth_base 表,无法获取唯一成长上限记录");
                    return RestResult.error(GlobalConsts.ERROR_NOT_ONLY_RESULT, "无法获取唯一成长上限记录");
                }
                if (null != list.get(0)) {
                    //因为是直接修改,所以不需要校验是否超过当天的成长值;
                    Long originalVal = mememberAction.getGrowthNumber();
                    Long finalGrowVal = originalVal + request.getGiveGrowthVal();

                    MememberGrowthLog mememberGrowthLog = new MememberGrowthLog();
                    mememberGrowthLog.setDeleteFlag(1);
                    mememberGrowthLog.setGrowthRuleVal(growthVal);
                    mememberGrowthLog.setMemberId(mememberAction.getId());
                    if (growthVal >= 0) {
                        mememberGrowthLog.setAddOrReduce(1);//成长值增加
                    } else {
                        mememberGrowthLog.setAddOrReduce(2);//成长值减少
                    }
                    mememberGrowthLog.setDeptId(deptId);
                    mememberGrowthLog.setUpdateTime(new Date());
//                    mememberGrowthLog.setUpdater(request.getUpdater());
//                    mememberGrowthLog.setUpdaterName(request.getUpdaterName());
                    mememberGrowthLog.setDescription("直接修改");
                    mememberGrowthLog.setDeptType(request.getDeptType());
                    mememberGrowthLog.setThirdUserId(Math.toIntExact(thirdUserId));
                    mememberGrowthLog.setChangeType(0L);
                    mememberGrowthLog.setGrowthNumber(growthVal);
                    long insert = mememberGrowthLogMapper.insert(mememberGrowthLog);
                    log.info("insert table - [mem_member_growth_log] info is:=========>,{}", JSON.toJSONString(mememberAction));
                    if (insert != 1) {
                        RestResult.error(GlobalConsts.ERROR_NOT_ONLY_RESULT, "新增会员成长值流水失败!");
                    }
                    MememberGrade mememberGrade = new MememberGrade();
                    mememberGrade.setGrowRangeStart(finalGrowVal);
                    mememberGrade.setGrowRangeEnd(finalGrowVal);
                    List<MememberGrade> gradeList = mememberGradeMapper.query(mememberGrade);
                    if (gradeList == null || gradeList.size() != 1) {
                        RestResult.error(GlobalConsts.ERROR_NOT_ONLY_RESULT, "无法获取唯一等级记录!");
                    }
                    MememberGrade grade = gradeList.get(0);

                    mememberAction.setGrowthNumber(finalGrowVal);
                    mememberAction.setDeptId(request.getDeptId());
                    long update = mememberUserMapper.update(mememberAction);

                    //调用用户旅程接口
                    List<Long> depts = new ArrayList<>();
                    depts.add(request.getDeptId());
                    EventListenerRequest eventListenerInputDTO = new EventListenerRequest();
                    eventListenerInputDTO.setDeptIds(depts);
                    eventListenerInputDTO.setEventType(1);
                    //计算会员等级升级/降级，级数
                    //查询升降级之前的会员等级
                    JSONObject json=new JSONObject();
                    MememberGrade oldMememberGrade = new MememberGrade();
                    mememberGrade.setGrowRangeStart(finalGrowVal);
                    mememberGrade.setGrowRangeEnd(finalGrowVal);
                    log.info("新增/消耗会员积分成长值==》查询会员等级表==》入参,{}",mememberGrade);
                    List<MememberGrade> oldGradeList = mememberGradeMapper.query(mememberGrade);
                    //判断升降级
                    if (finalGrowVal>originalVal){
                        eventListenerInputDTO.setEventCode("member_upgrade");
                        BigDecimal nowLevel = new BigDecimal(grade.getLevelCode());
                        BigDecimal oldLevel = new BigDecimal(oldGradeList.get(0).getLevelCode());
                        BigDecimal memberNum = nowLevel.subtract(oldLevel);
                        json.put("memberNum",memberNum);
                    }else {
                        eventListenerInputDTO.setEventCode("member_downgrade");
                        BigDecimal nowLevel = new BigDecimal(grade.getLevelCode());
                        BigDecimal oldLevel = new BigDecimal(oldGradeList.get(0).getLevelCode());
                        BigDecimal memberNum = nowLevel.subtract(oldLevel);
                        json.put("memberNum",memberNum);
                    }
                    eventListenerInputDTO.setThirdUserId(request.getMemberId()+"");
                    eventListenerInputDTO.setParam(json);
                    eventListenerInputDTO.setDeptType(request.getDeptType());
                    eventListenerInputDTO.setEventTime(System.currentTimeMillis());

                    log.info("Update table - [mem_member_action] info is:=========>,{}", JSON.toJSONString(mememberAction));
                    if (update != 1) {
                        RestResult.error(GlobalConsts.ERROR_NOT_ONLY_RESULT, "修改会员成长值失败!");
                    }

                    MemberGrowthActionApiResponse response = new MemberGrowthActionApiResponse();
                    BeanUtil.copy(mememberAction, response);
                    restResult.setData(response);
                    restResult.setCode(EXCEPTION.SUCCESS.getName());
                    restResult.setMsg(EXCEPTION.SUCCESS.getDesc());
                    return restResult;
                }
            } else {
                return RestResult.error(GlobalConsts.ERROR_NO_RESULT, "第三方用户Id为空");
            }
        } catch (Exception e) {
            log.info("Exception info is:=========>,{}", e.getMessage());
        }
        return restResult;
    }





    /**
     * 获取成长值规则主表信息
     *
     * @param mememberGrowthRule
     * @return
     */
    private RestResult getMememberGrowthRuleInfo(MememberGrowthRule mememberGrowthRule, MemberGrowthActionApiRequest memberGrowthActionApiRequest) {
        RestResult restResult = new RestResult();
        if (memberGrowthActionApiRequest.getDeptType() == 0) {
            mememberGrowthRule.setDeptType(6);
        }
        mememberGrowthRule.setObtainMode(memberGrowthActionApiRequest.getObtainMode());
        mememberGrowthRule.setStatus(0);
        List<MememberGrowthRule> list = mememberGrowthRuleMapper.query(mememberGrowthRule);
        if (null != list && list.size() > 0) {

        } else {
            restResult.setCode(EXCEPTION.FAIL.getName());
            restResult.setMsg(EXCEPTION.FAIL.getDesc());
        }
        log.info("query MememberGrowthRule info is =========》,{}", list);
        return null;
    }


    /**
     * 查询会员成长值列表
     * @param memberGrowthActionApiRequest 入参
     * @return
     */
    @Override
    public RestResult<MemberGrowthActionApiResponse> getMemberGrowthList(MemberGrowthActionApiRequest memberGrowthActionApiRequest) {
        RestResult restResult = new RestResult();
        MemberGrowthActionApiResponse memberGrowthActionApiResponse = new MemberGrowthActionApiResponse();
        log.info("query request info begin =========》,{}", memberGrowthActionApiRequest);
        log.info("开始查询会员成长值列表==》总入参,{}",memberGrowthActionApiRequest);
        if (memberGrowthActionApiRequest.getMemberId() ==null) {
            return RestResult.error(GlobalConsts.ERROR_CODE_PARAM_MISSING, "第三方用户Id为空");
        }
        if (StringUtils.isBlank(memberGrowthActionApiRequest.getBusinessType().toString())) {
            return RestResult.error(GlobalConsts.ERROR_CODE_PARAM_MISSING, "业务类型空");
        }
        //查询会员表
        MememberUser memberUser = new MememberUser();
        log.info("查询会员成长值列表==》查询会员表==》入参,{}",memberUser);
        List<MememberUser> userResult = mememberUserMapper.query(memberUser);
        log.info("查询会员成长值列表==》查询会员表==》结果,{}",userResult);
        if(CollectionUtils.isNotEmpty(userResult)){
            Long id = userResult.get(0).getId();
            //根据业务类型和业务编码查询
            SysDeptRequest sysDept=new SysDeptRequest();
            sysDept.setBusinessId(memberGrowthActionApiRequest.getBusinessCode());
            sysDept.setDeptType(memberGrowthActionApiRequest.getBusinessType().intValue());
            List<SysDeptResponse> resultSys = sysDeptApi.query(sysDept);
            MemberGrowthRuleRequest memberGrowthRule = new MemberGrowthRuleRequest();
            memberGrowthRule.setDeptId(resultSys.get(0).getId());
            memberGrowthRule.setDeptType(memberGrowthActionApiRequest.getBusinessType());
            //查询成长值主表
            log.info("查询会员成长值列表==》查询成长值主表==》入参,{}",memberGrowthRule);
            List<MememberGrowthRule> mememberGrowthRules = mememberGrowthRuleMapper.queryPage(memberGrowthActionApiRequest.getStart(),memberGrowthActionApiRequest.getLimit(),memberGrowthRule);
            log.info("查询会员成长值列表==》查询成长值主表==》结果,{}",JSON.toJSONString(mememberGrowthRules));
            List<MemberGrowthActionApiResponse> mememberGrowthApi = new ArrayList<MemberGrowthActionApiResponse> ();

            mememberGrowthRules.forEach(item->{
                memberGrowthActionApiResponse.setThirdUserId(id.toString());
                memberGrowthActionApiResponse.setGiveGrowthVal(item.getGiveGrowthVal());
                memberGrowthActionApiResponse.setDeptId(item.getDeptId());
                memberGrowthActionApiResponse.setObtainMode(item.getObtainMode());
            });
        }
        log.info("查询会员成长值列表==》返回值,{}",restResult);
        return restResult;
    }

    @Override
    public RestResult<PageResult<MemberGrowthRuleResponse>> queryStoreMemberPage(MemberGrowthRuleRequest memberGrowthRuleRequest) {
            AdminOperate adminOperate = memberGrowthRuleRequest.getAdminOperate();
            if (adminOperate.getDeptType().getCode()==1){
                memberGrowthRuleRequest.setDeptId(adminOperate.getDeptId());
            }
            //计算下标
            int startIndex = (memberGrowthRuleRequest.getStart() - 1) * memberGrowthRuleRequest.getLimit();
            List<MememberGrowthRule> list = mememberGrowthRuleMapper.queryPage(startIndex, memberGrowthRuleRequest.getLimit(), memberGrowthRuleRequest);
            list.forEach(items -> {
                RestResult<String> deptName = sysDeptApi.queryDeptNameById(items.getDeptId());
                items.setDeptName(deptName.getData());
            });
            //根据赠送方式获取成长值
            list.forEach(items -> {
                String obtainMode = items.getObtainMode();
                if (obtainMode.equals("1") || obtainMode.equals("4")) {
                    MememberGrowthOrderMoney mememberGrowthOrderMoney = mememberGrowthOrderMoneyService.getGiveGrowthValById(items.getId());
                    items.setGiveGrowthVal(mememberGrowthOrderMoney.getGiveGrowthVal());
                } else if (obtainMode.equals("2") || obtainMode.equals("5") || obtainMode.equals("6") || obtainMode.equals("7") || obtainMode.equals("8") || Long.valueOf(obtainMode) > 8) {
                    MememberGrowthAction mememberGrowthAction = mememberGrowthActionService.getGiveGrowthValById(items.getId());
                    items.setGiveGrowthVal(mememberGrowthAction.getGiveGrowthVal());
                } else if (obtainMode.equals("3")) {
                    MememberGrowthAppraisal mememberGrowthAppraisal = mememberGrowthAppraisalService.getGiveGrowthValById(items.getId());
                    items.setGiveGrowthVal(Long.valueOf(String.valueOf(mememberGrowthAppraisal.getGiveGrowthVal())));
                }
            });

            //根据赠送方式获取规则类型
            list.forEach(items -> {
                String obtainMode = items.getObtainMode();
                if (obtainMode.equals("1")) {
                    items.setRuleType("下单赠送");
                } else if (obtainMode.equals("2")) {
                    items.setRuleType("注册赠送");
                } else if (obtainMode.equals("3")) {
                    items.setRuleType("评价赠送");
                } else if (obtainMode.equals("4")) {
                    items.setRuleType("充值赠送");
                } else if (obtainMode.equals("5")) {
                    items.setRuleType("分享赠送");
                } else if (obtainMode.equals("6")) {
                    items.setRuleType("点赞赠送");
                } else if (obtainMode.equals("7")) {
                    items.setRuleType("晒单赠送");
                } else if (obtainMode.equals("8")) {
                    items.setRuleType("收藏赠送");
                }  else if (obtainMode.equals("9")) {
                    items.setRuleType("参与平台活动");
                } else if (obtainMode.equals("10")) {
                    items.setRuleType("发布活动");
                }
            });
            long count = mememberGrowthRuleMapper.queryPageCount(memberGrowthRuleRequest);
            PageResult pageResult = new PageResult();
            pageResult.setRows(list);
            pageResult.setTotal(count);
            RestResult result = new RestResult();
            result.setData(pageResult);
            result.setCode("0000");
            return result;

    }

    @Override
    public RestResult<Long> updateStoreGrowthRuleStatus(MemberGrowthRuleRequest request) {
        RestResult result = new RestResult();
        MememberGrowthRule mememberGrowthRule = new MememberGrowthRule();
        Integer deptType = request.getAdminOperate().getDeptType().getCode();
        if (deptType == 0) {
            mememberGrowthRule.setDeptType(6);
        } else if (deptType == 1) {
            mememberGrowthRule.setDeptType(1);
        } else if (deptType == 2) {
            mememberGrowthRule.setDeptType(2);
        } else if (deptType == 3) {
            mememberGrowthRule.setDeptType(3);
        }
        //判断是启用还是停用
        //如果是启用
        if (request.getStatus() == 0) {
            mememberGrowthRule.setObtainMode(request.getObtainMode());
            mememberGrowthRule.setStatus(0);
            //判断是否有其他同类型规则被启用
            long count = mememberGrowthRuleMapper.startRuleCount(mememberGrowthRule);
             //判断是否是自定义规则类型
                if (count == 1) {
                    result.setCode("5555");
                    result.setMsg("同类型规则只能启用一次，请先停用其他同类型规则");
                } else {
                    mememberGrowthRule.setId(request.getId());
                    long flag = mememberGrowthRuleMapper.updateGrowthRuleStatus(mememberGrowthRule);
                    result.setCode("0000");
                    result.setData(flag);
                }
        } else {
            mememberGrowthRule.setStatus(1);
            mememberGrowthRule.setId(request.getId());
            long flag = mememberGrowthRuleMapper.updateGrowthRuleStatus(mememberGrowthRule);
            result.setCode("0000");
            result.setData(flag);
        }
        return result;
    }

}
